package com.example.home.hospital.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.home.R;
import com.example.home.hospital.adapter.HospitalBannerAdapter;
import com.example.home.hospital.adapter.HospitalDetailAdapter;
import com.example.home.hospital.bean.BirefBean;
import com.example.home.hospital.bean.HospitalBanner;
import com.example.home.hospital.bean.HospitalDetailBanner;
import com.example.home.tool.OKHttpTool;

import java.util.Timer;
import java.util.TimerTask;

public class HospitalListDetail extends AppCompatActivity {

    private ImageView back;
    private ViewPager vpBanner;
    private TextView tvBrief;
    private Button btnGuahao;

    private String detailId;
    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;
    private HospitalDetailAdapter hospitalDetailAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_list_detail);
        Intent intent = getIntent();
        detailId = intent.getStringExtra("detailId");
        initView();

        //轮播图
        getBanner();
        startAutoScroll();

        //简介
        getBiref();

    }


    private void getBiref() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/hospital/" + detailId,
                OKHttpTool.GET,
                BirefBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        BirefBean birefBean = (BirefBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                tvBrief.setText( Html.fromHtml(birefBean.getData().getBrief()));
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/banner/list?hospitalId=" + detailId,
                OKHttpTool.GET,
                HospitalDetailBanner.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        HospitalDetailBanner hospitalBanner = (HospitalDetailBanner) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(hospitalBanner);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindBannershow(HospitalDetailBanner hospitalBanner) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        hospitalDetailAdapter = new HospitalDetailAdapter(HospitalListDetail.this, hospitalBanner.getData());
        viewPager.setAdapter(hospitalDetailAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == hospitalDetailAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }


    private void initView() {


        back = findViewById(R.id.back);
        vpBanner = findViewById(R.id.vp_banner);
        tvBrief = findViewById(R.id.tv_brief);
        btnGuahao = findViewById(R.id.btn_guahao);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HospitalListDetail.this.finish();
            }
        });
        btnGuahao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HospitalListDetail.this,PatientIDCard.class);
                startActivity(intent);
            }
        });

    }
}